package root.electronclass.electronclass.grade.honor

import android.os.Bundle
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.os.bundleOf
import androidx.core.view.updateLayoutParams
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import root.electronclass.electronclass.R
import root.electronclass.electronclass.adapter.GridSpacingItemDecoration
import root.electronclass.electronclass.common.base.BasicAdapter
import root.electronclass.electronclass.common.base.ViewBindingFragment
import root.electronclass.electronclass.databinding.FragmentClassHonorItemBinding
import root.electronclass.electronclass.databinding.FragmentClassListBinding
import root.electronclass.electronclass.databinding.FragmentClassListItemBinding
import root.electronclass.electronclass.ext.doOnSearch
import root.electronclass.electronclass.ext.dp
import root.electronclass.electronclass.ext.startFragment
import root.electronclass.electronclass.ext.tabs
import root.electronclass.electronclass.ext.toMMddnyyyy
import root.electronclass.electronclass.grade.model.ClassHonor

/**
 * 班级荣耀
 * @author Lokiy
 * @date 2023/2/5 13:19
 */
class ClassHonorFragment : ViewBindingFragment<FragmentClassListBinding>() {

    private val viewModel: ClassHonorViewModel by viewModels()

    @Suppress("UNCHECKED_CAST")
    private val adapter = BasicAdapter<Map.Entry<String, List<ClassHonor>>, FragmentClassListItemBinding> { holder, item ->
        holder.binding.vDash.updateLayoutParams<ConstraintLayout.LayoutParams> {
            topMargin = if (holder.absoluteAdapterPosition == 0) 12 else 0
        }
        holder.binding.itemTitle.text = item.key.toMMddnyyyy
        val childAdapter = BasicAdapter<ClassHonor, FragmentClassHonorItemBinding> { holder1, childItem ->
            holder1.binding.itemTitle.text = childItem.title
            holder1.binding.itemCorner.setText(if (childItem.type == 2) R.string.class_honor_team else R.string.class_honor_person)
            holder1.binding.itemDesc.text = childItem.belong
            holder1.binding.itemImage.setImageURI(childItem.picList?.getOrNull(0)?.url)
            holder1.itemView.setOnClickListener {
                startFragment(ClassHonorDetailFragment::class.java, bundleOf(EXTRA_DATA to childItem))
            }
        }
        holder.binding.itemRecycler.let { recyclerView ->
            recyclerView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                marginStart = 6
                marginEnd = 22
            }
            recyclerView.layoutManager = GridLayoutManager(context, 4)
            val tag = recyclerView.getTag(R.id.tab)
            if (tag == null) {
                recyclerView.addItemDecoration(GridSpacingItemDecoration(4, dp2px(24.dp).toInt(), false).also { recyclerView.setTag(R.id.tab, it) })
                recyclerView.updateLayoutParams<ConstraintLayout.LayoutParams> {
                    topMargin = dp2px(20.dp).toInt()
                }
            }
            recyclerView.adapter = childAdapter
        }
        childAdapter.refresh(item.value)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        val tabs = resources.getStringArray(R.array.class_tabs)
        binding.tabLayout.tabs(tabs) { tab ->
            val text = binding.search.text.toString()
            when (tab) {
                getString(R.string.class_7_day) -> viewModel.load(type = 1, text = text, 1)
                getString(R.string.class_3_month) -> viewModel.load(type = 2, text = text, 1)
                getString(R.string.class_current_semester) -> viewModel.load(type = 3, text = text, 1)
                getString(R.string.class_all) -> viewModel.load(type = 0, text = text, 1)
            }
        }

        binding.recycler.adapter = adapter
        binding.search.doOnSearch {
            viewModel.load(text = it, page = 1)
            false
        }

        viewModel.data.observe(viewLifecycleOwner) { result ->
            val list: List<ClassHonor> = if (result.first == 1) {
                result.second
            } else {
                adapter.list.toMutableList().flatMap { it.value } + result.second
            }
            adapter.notifyChanged(list.groupBy { it.time.trim().take(10) }.toMap().entries)
        }
    }

    override fun onHiddenChanged(hidden: Boolean) {
        super.onHiddenChanged(hidden)
        if (hidden.not() && view != null) {
            viewModel.load(page = 1)
        }
    }
}
